import Vue from "vue";

export default function(app) {
  /**
   * formatDate() return 格式化后的时间字符串
   * 根据传入的时间戳返回格式化后的时间字符串
   *
   * @param {Number} time 时间戳,可以不用传入，不传时返回当前时间字符串
   * @return {String} 格式化后的时间字符串，例如：'2019-07-11'
   */
	// eslint-disable-next-line no-multi-assign
	Vue.formatYMD = Vue.prototype.$formatDate = function (time) {
		const theDate = time ? new Date(time) : new Date();
		const year = theDate.getFullYear();
		const month = ('0' + (theDate.getMonth() + 1)).slice(-2);
		const date = ('0' + theDate.getDate()).slice(-2);
		return year + '-' + month + '-' + date;
    };
    
    /**
   * formatDateTime() return 格式化后的时间字符串
   * 根据传入的时间戳返回格式化后的时间字符串
   *
   * @param {Number} time 时间戳,可以不用传入，不传时返回当前时间字符串
   * @return {String} 格式化后的时间字符串，例如：'2019-07-11 13:57:06'
   */
	// eslint-disable-next-line no-multi-assign
	Vue.formatDateTime = Vue.prototype.$formatDateTime = function (time) {
		const theDate = time ? new Date(time) : new Date();
		const year = theDate.getFullYear();
		const month = ('0' + (theDate.getMonth() + 1)).slice(-2);
		const date = ('0' + theDate.getDate()).slice(-2);
		const hour = ('0' + theDate.getHours()).slice(-2);
		const minute = ('0' + theDate.getMinutes()).slice(-2);
		const second = ('0' + theDate.getSeconds()).slice(-2);
		return (
			year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
		);
	};

	/**
   * exportFile() 根据传入的url导出文件
   *
   * @param {String} url 根据文件的url
   */
	// eslint-disable-next-line no-multi-assign
	Vue.exportExcel = Vue.prototype.$exportFile = function (url) {
		if ('download' in document.createElement('a')) {
			// 非IE下载
			const elink = document.createElement('a');
			elink.style.display = 'none';
			elink.href = url;
			elink.download = '下载';
			document.body.appendChild(elink);
			elink.click();
			URL.revokeObjectURL(elink.href); // 释放URL 对象
			document.body.removeChild(elink);
		} else {
			// IE10+下载
			window.open(url);
		}
	};

}
